package com.dp.appi.service.impl;

import com.dp.appi.dao.PayDao;
import com.dp.appi.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by bchen on 2016/6/20.
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class PayServiceImpl implements PayService {
    @Autowired
    private PayDao payDao;
    /**
     * 平台产品购买清单
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> buyMenuList(Integer appliesType) {
        return payDao.buyMenuList(appliesType);
    }

    @Override
    public Map<String, Object> getBuyMent(Long buyMentId, Integer appliesType) {
        return payDao.getBuyMent(buyMentId, appliesType);
    }

    @Override
    public void buySuccess(String user_id, String total_fee, String type, String people_num, String buy_menu_id, String finish_time) {

        Map<String, Object> buyMentMap = payDao.getBuyMent(Long.valueOf(buy_menu_id), Integer.valueOf(type));
        String vaild_time = buyMentMap.get("vaild_time").toString();
        String time_unit = buyMentMap.get("time_unit").toString();

        String sql = "select org_id from t_s_user_org where user_id=?";
        List<Map<String, Object>> lists = payDao.findLists(sql, new Object[]{user_id});
        if(lists.isEmpty()){
            payDao.upUser(vaild_time, time_unit, finish_time, user_id);
            payDao.upRoleUser(user_id);
        }else{
            Map<String, Object> m = lists.get(0);
            String departid = String.valueOf(m.get("org_id"));
            payDao.addBuyLog(user_id, total_fee, type, people_num, departid, buyMentMap);
            if("1".equals(type)){//个人
                payDao.upUser(vaild_time, time_unit, finish_time, user_id);
                payDao.upRoleUser(user_id);
            }else{//单位
                payDao.upDepart(departid, people_num, vaild_time, time_unit, finish_time);
            }
        }
    }
}
